<%@page contentType="text/html;charset=UTF-8" %><%@page import="be.destin.memolog.UsageMode" %><%@page import="be.destin.memolog.UsageLogger" %><%@page import="be.destin.memo.Log" %><%@page import="be.destin.memo.TagDef" %><%@page import="be.destin.memo.Tagged" %><%--
 --%><%@page import="be.destin.skos.core.SkosManager" %><%--
 --%><%@page import="be.destin.skos.html.SkosHtmlManager" %><%--
 --%><%@page import="be.destin.skos.html.SkosHtmlTable" %><%--
 --%><%@page import="be.destin.skos.core.SchemeInterface" %><%--
 --%><%@page import="be.destin.skos.core.SchemeUsage" %><%--
 --%><%@page import="be.destin.skos.core.ConceptScheme" %><%--
 --%><%@page import="be.destin.skos.core.Concept" %><%--
 --%><%@page import="be.destin.skos.core.Application" %><%--
 --%><%@page import="be.destin.skos.core.Term" %><%--
 --%><%@page import="be.destin.skos.search.SkosSearch" %><%--
 --%><%@page import="be.destin.skos.view.ViewConcept" %><%--
 --%><%@page import="be.destin.skos.view.SkosView" %><%--
 --%><%@page import="be.destin.skos.view.SkosViewType" %><%--
 --%><%@page import="be.destin.skos.view.SkosViewManager" %><%--
 --%><%@page import="be.destin.util.Util" %><%--
--%><%@page import="java.util.List" %><%--
--%><%@page import="java.util.LinkedList" %><%--
--%><%@page import="java.util.LinkedHashMap" %><%--
--%><%@page import="java.util.LinkedHashSet" %><%--
--%><%@page import="java.util.Map" %><%--
 --%><%@page import="java.util.HashSet" %><%--
 --%><%@page import="java.util.ArrayList" %><%--
 --%><%@page import="java.util.TreeMap" %><%--
 --%><%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %><%--
 --%><%--
 --%><%	 
   SkosHtmlManager skosHtmlManager = SkosHtmlManager.getAskosiSessionInstance(request);
   SkosManager skosManager = skosHtmlManager.getSkosManager();
   String directAbout = request.getParameter("directabout");
   boolean redirected = false;
   if (directAbout != null && !directAbout.isEmpty()) {
	   Concept aConcept = skosManager.getScheme_Concept(directAbout);
	   if (aConcept != null) {
		   response.sendRedirect(aConcept.getAskosiUrl(skosHtmlManager,""));
		   redirected = true;
	   }
   }
   if (!redirected) {

   String language = skosHtmlManager.getLanguage();
   String schemeCode = request.getParameter("scheme");
   if ("null".equals(schemeCode)) schemeCode = null;
   String sort = request.getParameter("sort");
	String query = request.getParameter("query");
	if (query==null) {
		query = (String) pageContext.getSession().getAttribute("query");
		if (query==null) query="";
	}
	pageContext.getSession().setAttribute("query",query);
   String basket = request.getParameter("basket");
   String leftTruncation = request.getParameter("leftTruncation");
   String byPieces = request.getParameter("byPieces");
   String independant = request.getParameter("independant");
   String framing = request.getParameter("framed");
   String agrovocTerms ="";
   String elisTerms ="";
   boolean framed = framing != null && framing.equalsIgnoreCase("yes");
   if (independant != null && independant.equalsIgnoreCase("yes")) framed = false;
   String strictlanguage = request.getParameter("strict");
   boolean strictLanguage = strictlanguage != null && strictlanguage.equalsIgnoreCase("on");
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
<html>
<head>
<link rel="shortcut icon" href="image/tulipevertepetite.png" >
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/css/jqueryui.css" />
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/css/askosi.css" />
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/css/ajaxtags.css" />
<%
   ConceptScheme aScheme = null;
   SkosHtmlTable skosHtmlTable = null;
   int nbColumns = 0;
   if (schemeCode != null && !schemeCode.isEmpty()) {
       aScheme = (ConceptScheme)skosManager.getScheme(schemeCode);
   }
   long searchTime = System.currentTimeMillis();
   ArrayList<Concept> result = null;
   if (query != null && !query.isEmpty()) {
		result = SkosSearch.search(skosHtmlManager,strictLanguage,(SchemeInterface)aScheme,query, ! "on".equals(leftTruncation), "on".equals(byPieces),null);
   } else if (basket != null) {
		query = "Your Basket!";
		HashSet<Concept> unsorted = skosManager.toSetScheme_Concept(basket,aScheme,null,null,true);
		TreeMap<String,Concept> sorter = new TreeMap<String,Concept>();
		for (Concept aConcept: unsorted) {
			String key = aConcept.getInScheme().getAbout()+' '+aConcept.getSortLabel(language);
			if (key != null) {
				sorter.put(key,aConcept);
			}
		}
		result = new ArrayList<Concept>(sorter.values());
   }
   if (result == null) {
		result = new ArrayList<Concept>();
   }
   searchTime = System.currentTimeMillis() - searchTime;
   int resultSize = 0;
   if (result != null && !result.isEmpty()) {
		resultSize = result.size();
   }
%>
<title>Search <%=query%> <%if(schemeCode != null) {%>within Scheme <%=schemeCode%><%}%>: <%=result.size()%> concepts</title>
    <script type="text/javascript" src="js/jquery-1.7.2.js"></script>
	<script type="text/javascript" src="js/jquery-ui-1.8.23.custom.js"></script>
    <script type="text/javascript" src="js/sessvars.js"></script>
    <script type="text/javascript" src="js/tablescroll.js"></script>
    <script type="text/javascript">
		sessvars.contextPath = "<%= request.getContextPath() %>";
		headerHeight=140;

		$(function() {
			$( "#project" ).autocomplete({
				minLength: 2,
				source: 'autocomplete.jsp?scheme=<%=schemeCode%>',
				focus: function( event, ui ) {
					$( "#project" ).val( ui.item.label );
					return false;
				},
				select: function( event, ui ) {
					$( "#project" ).val( ui.item.label );
					return false;
				}
			})
			.data( "autocomplete" )._renderItem = function( ul, item ) {
				return $( "<li></li>" )
					.data( "item.autocomplete", item )
					.append( "<a>" + item.label + "</a>" )
					.appendTo( ul );
			};
		});
	</script>
</head>
<body class="contentpage">
<div id="header">
<%String pageID = "SEARCH";%><%@include file="basket.jnc"%>
   <%@include file="searchbox.jnc"%>
</div>
<div id="subheader">
<%
   if(schemeCode == null || "".equals(schemeCode)) {
     if (resultSize > 0) {
		 int compte = 0;
		 ConceptScheme precScheme = null;
	     for(Concept aConcept: result) {
			if (aConcept.getInScheme() != precScheme) {
				if (compte > 0) {
					%><a class="linkaction" href="#result_<%=precScheme.getAbout()%>"><%=skosHtmlManager.toHtmlImage(precScheme.getIconAdapted(skosHtmlManager),precScheme.getAbout(),false)%><%=precScheme.getTitle(language)%>:<big><%=compte%></big></a> &nbsp; <%
				}
				precScheme = aConcept.getInScheme();
			  compte = 0;
			}
			compte++;
		 }
		 if (compte > 0) {
			%><a class="linkaction" href="#result_<%=precScheme.getAbout()%>"><%=skosHtmlManager.toHtmlImage(precScheme.getIconAdapted(skosHtmlManager),precScheme.getAbout(),false)%><%=precScheme.getTitle(language)%>:<big><%=compte%></big></a><br/><%
		 }
	 }
   }%></div>
<div id="content"><img src="image/carrevide.gif" height="140" border="0" alt="top margin"/><br style="clear:both"/>
<form action="search.jsp" method="GET" class="searchForm"><input type="hidden" name="scheme" value="<%=schemeCode%>"/><%--
	   --%><label for="leftTruncation" title="Left Truncation"><b>*</b><input type="checkbox" name="leftTruncation" value="on" <%
			if ("on".equals(leftTruncation)) {
				%>checked<%
			}%>/></label><input type="text" name="query" id="project" size="30" value="<%=query%>"/><b>*</b>&nbsp;<%--
	   	   --%><label for="byPieces" title="By Pieces"><input type="checkbox" name="byPieces" value="on" <%
			if ("on".equals(byPieces)) {
				%>checked<%
			}%>/></label><%--
	   --%><input type="image" src="image/search.png" name="Search" alt="Search"><%--
	--%></form>
<%
   if(schemeCode != null && !"".equals(schemeCode)) {
     %>within <img src="image/conceptscheme16.gif" border="0"/><a href="view.jsp?scheme=<%=schemeCode%>"><%=schemeCode%></a><%
   }%> = 
<% if (resultSize > 0) {
       %><big><img src="image/concept16.gif" border="0"/><%=resultSize%> concepts:</big><%
   } else {
       %> &nbsp;  <big>not found</big>
<% }%> &nbsp; <small>CPU time: <%=searchTime%>ms</small><br style="clear:both"/>
<% if (resultSize > 0) {
		 int compte = 0;
		 ConceptScheme precScheme = null;
		 char initiale = 0x00;
		 String[] QUERIES = Util.prepareHighlight(query,"on".equals(byPieces));
	     for(Concept aConcept: result) {
				    char currLetter = Util.noAccent((""+aConcept.getPrefLabel(language,false).charAt(0)).toUpperCase()).charAt(0);
					if (aConcept.getInScheme() != precScheme || currLetter != initiale) {
					  if (aConcept.getInScheme() != precScheme) {
						  precScheme = aConcept.getInScheme();
					      skosHtmlTable = new SkosHtmlTable(skosHtmlManager);
						  skosHtmlTable.setBasket(true);
						  nbColumns = skosHtmlTable.putScheme(precScheme);
						  if (compte > 0) {
						     %></table><%
							 if(!agrovocTerms.isEmpty()) {%>
<p style="background-color:#e0ffe0; border:solid 1px black; margin:6px; text-align:center"><a target="_blank" href="http://agris.fao.org/?query=agrovoc:(<%=agrovocTerms%>)">Search in AGRIS</a></p><%
								agrovocTerms="";
							 }
							 if(!elisTerms.isEmpty()) {%>
<p style="background-color:#e0ffe0; border:solid 1px black; margin:6px; text-align:center"><a target="_blank" href="http://eprints.rclis.org/simple-search?query=<%=elisTerms.substring(4)%>)">Search in E-LIS</a></p><%
								elisTerms="";
							 }
							 %></div><div style="clear:both;line-height:1px;padding:0px;margin:0px;"> &nbsp; </div><%
						  }
						  %><div style="position: absolute; margin-top:-140px;"><a name="result_<%=precScheme.getAbout()%>"></a></div>
						  <div style="border:solid 1px; padding:8px; margin:3px; -webkit-border-radius:3px;-moz-border-radius:3px;border-radius:3px; float:left; background-color:#ddddff;"><img src="image/conceptscheme16.gif" border="0"/> <big><%=skosHtmlManager.toHtmlImage(precScheme.getIconAdapted(skosHtmlManager),precScheme.getAbout(),false)%><%=precScheme.getTitle(language)%></big>
						  <table border="0" cellspacing="0" style="border-collapse:collapse; border:1px solid black; background-color:#ffffff;"><col/><col/><%=skosHtmlTable.toHtmlColGroups()%><%
					  }
					  initiale = currLetter;
					  skosHtmlTable.initRow();
					  %><tr class="<%=(skosHtmlTable.nextRowClass())%>">
						<th align="left">&nbsp;</th>
						<th align="left">[<%=initiale%>]</th>
						<%=skosHtmlTable.toHtmlApplicationHeaders()%></tr>
						<tr class="<%=skosHtmlTable.nextRowClass()%>">
						<th align="left">&nbsp;</th>
						<th align="left">&nbsp;</th>
					  <%=skosHtmlTable.toHtmlRoleHeaders()%></tr><%
					}
			compte++;
		    String display = aConcept.toHtmlLabel(skosHtmlManager);
			if ("c".equals(aConcept.getInScheme().getAbout())) agrovocTerms = agrovocTerms+" %22"+aConcept.getPrefLabel("en",false)+"%22";
			if ("jita".equals(aConcept.getInScheme().getAbout())) elisTerms = elisTerms+" OR %22"+aConcept.getPrefLabel("en",false)+"%22";
		    if (QUERIES != null) display = Util.highlight(display,QUERIES);
			%><tr class="<%=skosHtmlTable.nextRowClass()%>">
				<td class="SkosStatusConcept">
					<%=aConcept.toHtmlStatus(skosHtmlManager)%>
				</td>
				<td class="SkosTableConcept"><%=display%></td>
			<%=skosHtmlTable.toHtmlColumns(aConcept)%></tr><%
		 }%></table><%
		 if(!agrovocTerms.isEmpty()) {%>
<p style="background-color:#e0ffe0; border:solid 1px black; margin:6px; text-align:center"><a target="_blank" href="http://agris.fao.org/?query=agrovoc:(<%=agrovocTerms%>)">Search in AGRIS</a></p><%
			agrovocTerms="";
		 }
		 if(!elisTerms.isEmpty()) {%>
<p style="background-color:#e0ffe0; border:solid 1px black; margin:6px; text-align:center"><a target="_blank" href="http://eprints.rclis.org/simple-search?query=<%=elisTerms.substring(4)%>">Search in E-LIS</a></p><%
			elisTerms="";
		 }
		 %></div><%
    }%>
<br style="clear:both"/>Thesaurus navigation and maintenance, Indexation browsing are services developed in the <a href="http://www.askosi.org">ASKOSI</a> project.
</div>
</body></html><%
}%>